package com.platform.service;

import com.platform.dao.ApiUserMapper;
import com.platform.entity.UserVo;
import com.platform.util.wechat.MD5;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.platform.dao.ApiUserIntegralMapper;
import com.platform.entity.UserIntegralVo;

import static com.platform.config.IntegralType.WARN_CLASS_INTEGRAL_TYPE;


@Service
public class ApiUserIntegralService {
    private Logger logger = Logger.getLogger(getClass());


    @Autowired
    private ApiUserIntegralMapper userIntegralMapper;
    @Autowired
    private ApiUserMapper userMapper;


    public UserIntegralVo queryObject(Integer id) {
        return userIntegralMapper.queryObject(id);
    }


    public List<UserIntegralVo> queryList(Map<String, Object> map) {
        return userIntegralMapper.queryList(map);
    }


    public int queryTotal(Map<String, Object> map) {
        return userIntegralMapper.queryTotal(map);
    }


    public int save(UserIntegralVo userIntegral) {
        return userIntegralMapper.save(userIntegral);
    }


    public int update(UserIntegralVo userIntegral) {
        return userIntegralMapper.update(userIntegral);
    }


    public int delete(Integer id) {
        return userIntegralMapper.delete(id);
    }


    public int deleteBatch(Integer[] ids) {
        return userIntegralMapper.deleteBatch(ids);
    }


    /**
     * 查询是否有暖课消耗记录
     *
     * @param warmClassId 暖课id
     * @param userId      用户id
     * @return
     */
    public Boolean warmClassIsPay(Integer warmClassId, Long userId) {
        Boolean flag = true;
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        //此处的orderId作为暖课的id
        params.put("orderId", warmClassId);
        //状态:1-增加；2-减少
        params.put("status", 2);
        params.put("integralType", WARN_CLASS_INTEGRAL_TYPE.getIndex());
        int i = queryTotal(params);
        if (i == 0) {
            flag = false;
        }
        return flag;
    }

    /**
     * 计算当前用户的积分
     *
     * @param userId   用户
     * @param status   状态:1-增加；2-减少
     * @param integral 积分
     * @return
     */
    public boolean calUserCurrentPoints(Long userId, int status, Integer integral) {

        if (integral == null) {
            logger.error("积分为空");
            return false;
        }

        if (integral == 0) {
            logger.error("积分为0");
            return false;
        }

        //先加密目前积分
        UserVo userVo = userMapper.queryObject(userId);
        if (userVo == null) {
            logger.error("找不到用户");
            return false;
        }

        Integer userVoIntegral = Integer.valueOf(userVo.getIntegral());
        if (userVoIntegral != 0) {
            String messageDigest = MD5.getMessageDigest(userVoIntegral.toString());
            if (messageDigest == null) {
                logger.info("计算加密值异常");
                return false;
            }
            if (userVo.getIntegral_encryption() == null) {
                logger.info("用户积分加密值为空");
                return false;
            }
            //对比两者的加密值是否一样
            if (messageDigest.equals(userVo.getIntegral_encryption())) {
                //一样则根据参数来进行增加或者减少，然后再加密
                if (status == 1) {
                    userVo.setIntegral(String.valueOf(userVoIntegral + integral));
                } else if (status == 2) {
                    userVo.setIntegral(String.valueOf(userVoIntegral - integral));
                }
                userVo.setIntegral_encryption(MD5.getMessageDigest(userVo.getIntegral()));
                userMapper.update(userVo);
            }
        } else {
            //无需判断直接计算
            if (status == 1) {
                userVo.setIntegral(String.valueOf(userVoIntegral + integral));
            } else if (status == 2) {
                userVo.setIntegral(String.valueOf(userVoIntegral - integral));
            }
            userVo.setIntegral_encryption(MD5.getMessageDigest(userVo.getIntegral()));
            userMapper.update(userVo);
        }
        return true;
    }

    BigDecimal queryTotalIntegral(Map<String, Object> params) {
        return userIntegralMapper.queryTotalIntegral(params);
    }
}
